{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Action1_使用libfm工具对movielens进行评分预测，采用SGD优化算法\n",
    "\n",
    "1、运行出结果，结果正确（10points）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "perl triple_format_to_libfm.pl -in ratings.csv -target 2 -delete_column 3 -separator \",\"\n",
    "\n",
    "生成：ratings.csv.libfm\n",
    "\n",
    "然后再用libfm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------------------------------------------\n",
      "libFM\n",
      "  Version: 1.40\n",
      "  Author:  Steffen Rendle, steffen.rendle@uni-konstanz.de\n",
      "  WWW:     http://www.libfm.org/\n",
      "  License: Free for academic use. See license.txt.\n",
      "----------------------------------------------------------------------------\n",
      "Loading train...\t\n",
      "has x = 0\n",
      "has xt = 1\n",
      "num_rows=1048575\tnum_values=2097150\tnum_features=21146\tmin_target=0.5\tmax_target=5\n",
      "Loading test... \t\n",
      "has x = 0\n",
      "has xt = 1\n",
      "num_rows=1048575\tnum_values=2097150\tnum_features=21146\tmin_target=0.5\tmax_target=5\n",
      "#relations: 0\n",
      "Loading meta data...\t\n",
      "#Iter=  0\tTrain=2.25797\tTest=2.25797\n",
      "#Iter=  1\tTrain=1.0487\tTest=1.47386\n",
      "#Iter=  2\tTrain=0.870806\tTest=1.18129\n",
      "#Iter=  3\tTrain=0.852513\tTest=1.05034\n",
      "#Iter=  4\tTrain=0.850301\tTest=0.981663\n",
      "#Iter=  5\tTrain=0.849794\tTest=0.941676\n",
      "#Iter=  6\tTrain=0.849307\tTest=0.916584\n",
      "#Iter=  7\tTrain=0.848373\tTest=0.899698\n",
      "#Iter=  8\tTrain=0.846568\tTest=0.887634\n",
      "#Iter=  9\tTrain=0.843098\tTest=0.878377\n",
      "#Iter= 10\tTrain=0.838754\tTest=0.870874\n",
      "#Iter= 11\tTrain=0.833223\tTest=0.864369\n",
      "#Iter= 12\tTrain=0.826047\tTest=0.858267\n",
      "#Iter= 13\tTrain=0.817829\tTest=0.852237\n",
      "#Iter= 14\tTrain=0.811989\tTest=0.846543\n",
      "#Iter= 15\tTrain=0.808194\tTest=0.841307\n",
      "#Iter= 16\tTrain=0.805606\tTest=0.836616\n",
      "#Iter= 17\tTrain=0.803414\tTest=0.83239\n",
      "#Iter= 18\tTrain=0.80175\tTest=0.828607\n",
      "#Iter= 19\tTrain=0.799914\tTest=0.82515\n",
      "#Iter= 20\tTrain=0.797736\tTest=0.821981\n",
      "#Iter= 21\tTrain=0.795291\tTest=0.819002\n",
      "#Iter= 22\tTrain=0.792866\tTest=0.816157\n",
      "#Iter= 23\tTrain=0.790331\tTest=0.813432\n",
      "#Iter= 24\tTrain=0.787994\tTest=0.810814\n",
      "#Iter= 25\tTrain=0.786233\tTest=0.808334\n",
      "#Iter= 26\tTrain=0.784588\tTest=0.80599\n",
      "#Iter= 27\tTrain=0.78295\tTest=0.80375\n",
      "#Iter= 28\tTrain=0.781112\tTest=0.801593\n",
      "#Iter= 29\tTrain=0.779592\tTest=0.799539\n",
      "#Iter= 30\tTrain=0.778003\tTest=0.797563\n",
      "#Iter= 31\tTrain=0.776537\tTest=0.795652\n",
      "#Iter= 32\tTrain=0.775118\tTest=0.793817\n",
      "#Iter= 33\tTrain=0.773977\tTest=0.792059\n",
      "#Iter= 34\tTrain=0.772635\tTest=0.790367\n",
      "#Iter= 35\tTrain=0.771618\tTest=0.788737\n",
      "#Iter= 36\tTrain=0.770163\tTest=0.787143\n",
      "#Iter= 37\tTrain=0.76877\tTest=0.785602\n",
      "#Iter= 38\tTrain=0.767809\tTest=0.784111\n",
      "#Iter= 39\tTrain=0.767279\tTest=0.782677\n",
      "#Iter= 40\tTrain=0.766781\tTest=0.781305\n",
      "#Iter= 41\tTrain=0.765899\tTest=0.779982\n",
      "#Iter= 42\tTrain=0.765601\tTest=0.778709\n",
      "#Iter= 43\tTrain=0.765103\tTest=0.77749\n",
      "#Iter= 44\tTrain=0.765097\tTest=0.776331\n",
      "#Iter= 45\tTrain=0.764907\tTest=0.775226\n",
      "#Iter= 46\tTrain=0.764575\tTest=0.774168\n",
      "#Iter= 47\tTrain=0.764228\tTest=0.773156\n",
      "#Iter= 48\tTrain=0.764168\tTest=0.77219\n",
      "#Iter= 49\tTrain=0.764218\tTest=0.771272\n",
      "#Iter= 50\tTrain=0.764177\tTest=0.770397\n",
      "#Iter= 51\tTrain=0.763811\tTest=0.769555\n",
      "#Iter= 52\tTrain=0.763909\tTest=0.768746\n",
      "#Iter= 53\tTrain=0.763551\tTest=0.767972\n",
      "#Iter= 54\tTrain=0.763632\tTest=0.767234\n",
      "#Iter= 55\tTrain=0.763455\tTest=0.766518\n",
      "#Iter= 56\tTrain=0.763335\tTest=0.765838\n",
      "#Iter= 57\tTrain=0.763295\tTest=0.765184\n",
      "#Iter= 58\tTrain=0.763528\tTest=0.764564\n",
      "#Iter= 59\tTrain=0.763696\tTest=0.763972\n",
      "#Iter= 60\tTrain=0.763315\tTest=0.763398\n",
      "#Iter= 61\tTrain=0.76324\tTest=0.762848\n",
      "#Iter= 62\tTrain=0.763243\tTest=0.762315\n",
      "#Iter= 63\tTrain=0.763463\tTest=0.761802\n",
      "#Iter= 64\tTrain=0.763448\tTest=0.761308\n",
      "#Iter= 65\tTrain=0.762975\tTest=0.760829\n",
      "#Iter= 66\tTrain=0.763025\tTest=0.760367\n",
      "#Iter= 67\tTrain=0.762801\tTest=0.759918\n",
      "#Iter= 68\tTrain=0.762396\tTest=0.75948\n",
      "#Iter= 69\tTrain=0.762219\tTest=0.759053\n",
      "#Iter= 70\tTrain=0.761773\tTest=0.758627\n",
      "#Iter= 71\tTrain=0.760896\tTest=0.758199\n",
      "#Iter= 72\tTrain=0.759994\tTest=0.757769\n",
      "#Iter= 73\tTrain=0.759314\tTest=0.757334\n",
      "#Iter= 74\tTrain=0.758419\tTest=0.756893\n",
      "#Iter= 75\tTrain=0.757061\tTest=0.756439\n",
      "#Iter= 76\tTrain=0.756495\tTest=0.755981\n",
      "#Iter= 77\tTrain=0.755513\tTest=0.755517\n",
      "#Iter= 78\tTrain=0.754742\tTest=0.755042\n",
      "#Iter= 79\tTrain=0.754081\tTest=0.754565\n",
      "#Iter= 80\tTrain=0.753379\tTest=0.754087\n",
      "#Iter= 81\tTrain=0.752571\tTest=0.753601\n",
      "#Iter= 82\tTrain=0.751703\tTest=0.753116\n",
      "#Iter= 83\tTrain=0.751432\tTest=0.752632\n",
      "#Iter= 84\tTrain=0.751075\tTest=0.75215\n",
      "#Iter= 85\tTrain=0.750594\tTest=0.751676\n",
      "#Iter= 86\tTrain=0.75028\tTest=0.751205\n",
      "#Iter= 87\tTrain=0.749613\tTest=0.750736\n",
      "#Iter= 88\tTrain=0.74962\tTest=0.750276\n",
      "#Iter= 89\tTrain=0.749499\tTest=0.74983\n",
      "#Iter= 90\tTrain=0.749224\tTest=0.749388\n",
      "#Iter= 91\tTrain=0.749197\tTest=0.748958\n",
      "#Iter= 92\tTrain=0.749072\tTest=0.748534\n",
      "#Iter= 93\tTrain=0.748995\tTest=0.74812\n",
      "#Iter= 94\tTrain=0.74907\tTest=0.747714\n",
      "#Iter= 95\tTrain=0.749057\tTest=0.747316\n",
      "#Iter= 96\tTrain=0.749122\tTest=0.746931\n",
      "#Iter= 97\tTrain=0.748777\tTest=0.746548\n",
      "#Iter= 98\tTrain=0.748676\tTest=0.746173\n",
      "#Iter= 99\tTrain=0.748828\tTest=0.74581\n",
      "Wall time: 3min 19s\n"
     ]
    }
   ],
   "source": [
    "# libfm，-method 默认为MCMC\n",
    "%time !libFM -task r -train ratings.csv.libfm -test ratings.csv.libfm -dim '1,1,8' -out movielens_out.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['3.65713',\n",
       " '3.79098',\n",
       " '3.87615',\n",
       " '4.01346',\n",
       " '4.27634',\n",
       " '3.67283',\n",
       " '3.63915',\n",
       " '3.65859',\n",
       " '3.70919',\n",
       " '4.25623']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('movielens_out.txt','r') as f1:\n",
    "    data1 = f1.read()\n",
    "data1.split()[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------------------------------------------\n",
      "libFM\n",
      "  Version: 1.40\n",
      "  Author:  Steffen Rendle, steffen.rendle@uni-konstanz.de\n",
      "  WWW:     http://www.libfm.org/\n",
      "  License: Free for academic use. See license.txt.\n",
      "----------------------------------------------------------------------------\n",
      "Loading train...\t\n",
      "has x = 1\n",
      "has xt = 0\n",
      "num_rows=1048575\tnum_values=2097150\tnum_features=21146\tmin_target=0.5\tmax_target=5\n",
      "Loading test... \t\n",
      "has x = 1\n",
      "has xt = 0\n",
      "num_rows=1048575\tnum_values=2097150\tnum_features=21146\tmin_target=0.5\tmax_target=5\n",
      "#relations: 0\n",
      "Loading meta data...\t\n",
      "learnrate=0.01\n",
      "learnrates=0.01,0.01,0.01\n",
      "#iterations=100\n",
      "SGD: DON'T FORGET TO SHUFFLE THE ROWS IN TRAINING DATA TO GET THE BEST RESULTS.\n",
      "#Iter=  0\tTrain=1.35698\tTest=1.35698\n",
      "#Iter=  1\tTrain=1.10162\tTest=1.10162\n",
      "#Iter=  2\tTrain=0.978962\tTest=0.978962\n",
      "#Iter=  3\tTrain=0.906216\tTest=0.906216\n",
      "#Iter=  4\tTrain=0.868454\tTest=0.868454\n",
      "#Iter=  5\tTrain=0.843682\tTest=0.843682\n",
      "#Iter=  6\tTrain=0.825132\tTest=0.825132\n",
      "#Iter=  7\tTrain=0.810279\tTest=0.810279\n",
      "#Iter=  8\tTrain=0.798121\tTest=0.798121\n",
      "#Iter=  9\tTrain=0.788072\tTest=0.788072\n",
      "#Iter= 10\tTrain=0.779694\tTest=0.779694\n",
      "#Iter= 11\tTrain=0.772647\tTest=0.772647\n",
      "#Iter= 12\tTrain=0.766659\tTest=0.766659\n",
      "#Iter= 13\tTrain=0.761519\tTest=0.761519\n",
      "#Iter= 14\tTrain=0.757057\tTest=0.757057\n",
      "#Iter= 15\tTrain=0.753148\tTest=0.753148\n",
      "#Iter= 16\tTrain=0.749692\tTest=0.749692\n",
      "#Iter= 17\tTrain=0.746614\tTest=0.746614\n",
      "#Iter= 18\tTrain=0.743853\tTest=0.743853\n",
      "#Iter= 19\tTrain=0.741362\tTest=0.741362\n",
      "#Iter= 20\tTrain=0.739101\tTest=0.739101\n",
      "#Iter= 21\tTrain=0.737041\tTest=0.737041\n",
      "#Iter= 22\tTrain=0.735154\tTest=0.735154\n",
      "#Iter= 23\tTrain=0.73342\tTest=0.73342\n",
      "#Iter= 24\tTrain=0.731819\tTest=0.731819\n",
      "#Iter= 25\tTrain=0.730337\tTest=0.730337\n",
      "#Iter= 26\tTrain=0.72896\tTest=0.72896\n",
      "#Iter= 27\tTrain=0.727678\tTest=0.727678\n",
      "#Iter= 28\tTrain=0.72648\tTest=0.72648\n",
      "#Iter= 29\tTrain=0.725359\tTest=0.725359\n",
      "#Iter= 30\tTrain=0.724307\tTest=0.724307\n",
      "#Iter= 31\tTrain=0.723318\tTest=0.723318\n",
      "#Iter= 32\tTrain=0.722386\tTest=0.722386\n",
      "#Iter= 33\tTrain=0.721506\tTest=0.721506\n",
      "#Iter= 34\tTrain=0.720673\tTest=0.720673\n",
      "#Iter= 35\tTrain=0.719885\tTest=0.719885\n",
      "#Iter= 36\tTrain=0.719136\tTest=0.719136\n",
      "#Iter= 37\tTrain=0.718425\tTest=0.718425\n",
      "#Iter= 38\tTrain=0.717748\tTest=0.717748\n",
      "#Iter= 39\tTrain=0.717103\tTest=0.717103\n",
      "#Iter= 40\tTrain=0.716488\tTest=0.716488\n",
      "#Iter= 41\tTrain=0.7159\tTest=0.7159\n",
      "#Iter= 42\tTrain=0.715337\tTest=0.715337\n",
      "#Iter= 43\tTrain=0.714799\tTest=0.714799\n",
      "#Iter= 44\tTrain=0.714283\tTest=0.714283\n",
      "#Iter= 45\tTrain=0.713788\tTest=0.713788\n",
      "#Iter= 46\tTrain=0.713312\tTest=0.713312\n",
      "#Iter= 47\tTrain=0.712855\tTest=0.712855\n",
      "#Iter= 48\tTrain=0.712416\tTest=0.712416\n",
      "#Iter= 49\tTrain=0.711993\tTest=0.711993\n",
      "#Iter= 50\tTrain=0.711585\tTest=0.711585\n",
      "#Iter= 51\tTrain=0.711192\tTest=0.711192\n",
      "#Iter= 52\tTrain=0.710812\tTest=0.710812\n",
      "#Iter= 53\tTrain=0.710446\tTest=0.710446\n",
      "#Iter= 54\tTrain=0.710092\tTest=0.710092\n",
      "#Iter= 55\tTrain=0.70975\tTest=0.70975\n",
      "#Iter= 56\tTrain=0.709419\tTest=0.709419\n",
      "#Iter= 57\tTrain=0.709099\tTest=0.709099\n",
      "#Iter= 58\tTrain=0.708789\tTest=0.708789\n",
      "#Iter= 59\tTrain=0.708489\tTest=0.708489\n",
      "#Iter= 60\tTrain=0.708197\tTest=0.708197\n",
      "#Iter= 61\tTrain=0.707915\tTest=0.707915\n",
      "#Iter= 62\tTrain=0.707641\tTest=0.707641\n",
      "#Iter= 63\tTrain=0.707374\tTest=0.707374\n",
      "#Iter= 64\tTrain=0.707116\tTest=0.707116\n",
      "#Iter= 65\tTrain=0.706864\tTest=0.706864\n",
      "#Iter= 66\tTrain=0.70662\tTest=0.70662\n",
      "#Iter= 67\tTrain=0.706382\tTest=0.706382\n",
      "#Iter= 68\tTrain=0.706151\tTest=0.706151\n",
      "#Iter= 69\tTrain=0.705926\tTest=0.705926\n",
      "#Iter= 70\tTrain=0.705707\tTest=0.705707\n",
      "#Iter= 71\tTrain=0.705494\tTest=0.705494\n",
      "#Iter= 72\tTrain=0.705286\tTest=0.705286\n",
      "#Iter= 73\tTrain=0.705083\tTest=0.705083\n",
      "#Iter= 74\tTrain=0.704886\tTest=0.704886\n",
      "#Iter= 75\tTrain=0.704693\tTest=0.704693\n",
      "#Iter= 76\tTrain=0.704505\tTest=0.704505\n",
      "#Iter= 77\tTrain=0.704322\tTest=0.704322\n",
      "#Iter= 78\tTrain=0.704143\tTest=0.704143\n",
      "#Iter= 79\tTrain=0.703968\tTest=0.703968\n",
      "#Iter= 80\tTrain=0.703797\tTest=0.703797\n",
      "#Iter= 81\tTrain=0.70363\tTest=0.70363\n",
      "#Iter= 82\tTrain=0.703467\tTest=0.703467\n",
      "#Iter= 83\tTrain=0.703308\tTest=0.703308\n",
      "#Iter= 84\tTrain=0.703152\tTest=0.703152\n",
      "#Iter= 85\tTrain=0.703\tTest=0.703\n",
      "#Iter= 86\tTrain=0.702851\tTest=0.702851\n",
      "#Iter= 87\tTrain=0.702705\tTest=0.702705\n",
      "#Iter= 88\tTrain=0.702563\tTest=0.702563\n",
      "#Iter= 89\tTrain=0.702423\tTest=0.702423\n",
      "#Iter= 90\tTrain=0.702287\tTest=0.702287\n",
      "Wall time: 1min 45s#Iter= 91\tTrain=0.702153\tTest=0.702153\n",
      "#Iter= 92\tTrain=0.702022\tTest=0.702022\n",
      "\n",
      "#Iter= 93\tTrain=0.701894\tTest=0.701894\n",
      "#Iter= 94\tTrain=0.701768\tTest=0.701768\n",
      "#Iter= 95\tTrain=0.701645\tTest=0.701645\n",
      "#Iter= 96\tTrain=0.701524\tTest=0.701524\n",
      "#Iter= 97\tTrain=0.701406\tTest=0.701406\n",
      "#Iter= 98\tTrain=0.70129\tTest=0.70129\n",
      "#Iter= 99\tTrain=0.701176\tTest=0.701176\n",
      "Final\tTrain=0.701176\tTest=0.701176\n"
     ]
    }
   ],
   "source": [
    "# libfm，有sgd\n",
    "%time !libFM -task r -train ratings.csv.libfm -test ratings.csv.libfm -dim '1,1,8' -method sgd -learn_rate 0.01 -regular ’0,0,0.01’ -init_stdev 0.1 -out movielens_out2.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['3.5214',\n",
       " '3.71206',\n",
       " '3.88674',\n",
       " '4.07321',\n",
       " '4.09841',\n",
       " '3.59604',\n",
       " '3.68465',\n",
       " '3.19724',\n",
       " '3.95619',\n",
       " '4.26663']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('movielens_out2.txt','r') as f2:\n",
    "    data2 = f2.read()\n",
    "data2.split()[:10]        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.0 64-bit ('Bi_env': venv)",
   "language": "python",
   "name": "python38064bitbienvvenvba07af95a1bb4b078aa8134bba84dff2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
